# -*- coding:utf-8 -*-
from flask import Flask, request, g, render_template
import sqlite3
import os

DATABASE = "test.db"

def get_db():
    db = getattr(g, '_database', None)
    if db is None:
        db = g._database = sqlite3.connect(DATABASE)
    return db

app = Flask(__name__)
app.config.from_object(__name__)

@app.teardown_appcontext
def close_connection(exception):
    db = getattr(g, '_database', None)
    if db is not None:
        db.close()

@app.route('/')
def index():
    cur = get_db().cursor()
    res = cur.execute("select * from COMPANY")
    entries = [dict(id=row[0], name=row[1], age=row[2]) for row in res.fetchall()]
    # print(entries)
    return render_template('index.html', entries=entries)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8002, threaded=True)